package com.woniu.userassembly.controller;

import com.woniu.feignClient.PayCallBackClient;
import com.woniu.ly.model.model.*;
import com.woniu.userassembly.model.info.UserSeckillInfo;
import com.woniu.userassembly.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@Slf4j
public class PayCallBack implements PayCallBackClient {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private UserService userService;

    public String testDemo(){
        return "支付成功";
    }
    public void testInDemo(HttpServletRequest httpRequest){
        log.info("执行内网穿透");
        Map<String, String[]> hashMap = new HashMap<>();
        hashMap = httpRequest.getParameterMap();
        String[] tradeNo = hashMap.get("out_trade_no");
        int orderId = Integer.parseInt(tradeNo[0]);
        // 查询团类型（判断执行不同方法）
        UserOrder userOrder = userService.selectOrderById(orderId);
        String tourGenre = userOrder.getTourGenre();
        if (tourGenre.equals("秒杀团")){
            //成功后修改用户缓存的支付状态
            HashOperations operations = redisTemplate.opsForHash();
            com.woniu.ly.model.model.UserOrder order = userService.findUserOrderById(orderId);
            int userId = order.getUserId();
            int tourId = order.getTourId();
            log.info("userId: {} , tourId: {}",userId,tourId);
            UserSeckillInfo userSeckillInfo = (UserSeckillInfo)operations.get("AfterSuccess"+ tourId,"userSeckillInfo"+ userId);
            userSeckillInfo.setPayStuats("yes");
            int travelNum = userSeckillInfo.getTravelNum();
            operations.put("AfterSuccess"+ tourId,"userSeckillInfo"+ userId,userSeckillInfo);
            //支付成功，将名额添加到数据库
            SeckillTour seckillTour = userService.selectSeckillTourById(tourId);
            seckillTour.setTourNowNum(seckillTour.getTourNowNum()+travelNum);
            userService.updateSeckillTourNowNum(seckillTour);
            //支付成功修改订单状态
            userService.changeOrderStatus(orderId,"已付款");
        }
        if (tourGenre.equals("普通团")){
            userService.pay(orderId);
            com.woniu.userassembly.model.UserAccount userAccount=new com.woniu.userassembly.model.UserAccount();
            userAccount.setUserId(userOrder.getUserId());
            userAccount.setUserAccountContent("订单付款成功");
            userAccount.setUserAccountMoney(userOrder.getUserOrderPrice());
            userAccount.setUserAccountTime(Timestamp.valueOf(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())));
            userAccount.setUserAccountType("付款");
            userService.userAccount(userAccount);
            //获取预订单数据
            System.out.println("异步回调付款成功");
            PlatfromStatement platfromStatement =new PlatfromStatement();
            platfromStatement.setStatementType("用户下单");
            platfromStatement.setStatementTime(Timestamp.valueOf(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())));
            platfromStatement.setStatementAmount(userOrder.getUserOrderPrice());
            platfromStatement.setOrderType("用户");
            platfromStatement.setOrderId(userOrder.getUserOrderId());
            userService.platformStatement(platfromStatement);
            double v = userService.selectLastPlatformAccount();
            userService.platformAccount(userOrder.getUserOrderPrice()+v,platfromStatement.getStatementId());
        }



    }
}
